package com.example.szh.mvp.view.video;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.szh.R;
import com.example.szh.adapter.SmallAdapter;
import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.VideoNewsEntity;
import com.example.szh.http.HttpCode;
import com.example.szh.mvp.contract.LREContract;
import com.example.szh.mvp.di.DaggerLREConponent;
import com.example.szh.mvp.di.LREMoudes;
import com.example.szh.mvp.presenter.LREPresenter;
import com.example.szh.nextwork.view.BaseFragment;
import com.example.szh.utils.ToastUtils;
import com.example.szh.widget.VideoNewsItemDecoration;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashMap;
import java.util.Map;


public class SmallVideoFragment extends BaseFragment<LREPresenter> implements LREContract.ILREView ,
        OnRefreshLoadMoreListener, BaseQuickAdapter.OnItemClickListener {


    private SmartRefreshLayout smart;
    private RecyclerView rv;
    private GridLayoutManager manager;
    private SmallAdapter adapter;

    @Override
    public void refreshAll(BaseEntity entity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) entity;
        if (videoNewsEntity.getValues()==null){
            ToastUtils.showMsg("请求出错");
            return;
        }
        adapter.setNewData(videoNewsEntity.getValues());
    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) entity;
        LogUtils.json(videoNewsEntity);
        if (videoNewsEntity.getValues()==null){
            ToastUtils.showMsg("请求出错");
            return;
        }
        adapter.setNewData(videoNewsEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {
        VideoNewsEntity videoNewsEntity = (VideoNewsEntity) entity;
        if (videoNewsEntity.getValues()==null){
            ToastUtils.showMsg("最后一页");
            return;
        }
        adapter.addData(videoNewsEntity.getValues());
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_small_video;

    }

    @Override
    public void initView() {
        smart = f(R.id.small_smart);
        rv = f(R.id.small_rv);
        smart.setEnableRefresh(true);
        smart.setEnableLoadMore(true);
        smart.setRefreshHeader(new ClassicsHeader(getActivity()));
        smart.setRefreshFooter(new ClassicsFooter(getActivity()));
        smart.setOnRefreshLoadMoreListener(this);
        manager=new GridLayoutManager(getContext(),2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        adapter = new SmallAdapter(R.layout.item_small_video);
        adapter.setOnItemClickListener(this::onItemClick);
        rv.setAdapter(adapter);
        rv.setLayoutManager(manager);
        rv.addItemDecoration(new VideoNewsItemDecoration(getContext()));
    }

    @Override
    public void inject() {
        DaggerLREConponent.builder().lREMoudes(new LREMoudes(this)).build().injectSmallVideoFragment(this);
    }

    @Override
    public void initData() {
        Map<String,Object> map = new HashMap<>();
        map.put("news_type",0);
        map.put("code", HttpCode.VIDEONEWSCODE);
        p.requestAll(map);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        Map<String,Object>map = new HashMap<>();
        map.put("news_type",0);
        map.put("code", HttpCode.VIDEONEWSCODE);
        p.requestLoad(map);
        refreshLayout.finishLoadMore();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        Map<String,Object>map = new HashMap<>();
        map.put("news_type",0);
        map.put("code", HttpCode.VIDEONEWSCODE);
        p.requestRefresh(map);
        refreshLayout.finishRefresh();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ToastUtils.showMsg(""+position);
        startPage(VideoActivity.class);
    }
}